package com.gxs.ea.fileupload.beans.utils;

import java.io.Serializable;

import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import org.springframework.stereotype.Component;

@Component
public class JSFUtils  implements Serializable 
{

	public UIComponent findComponent(final String id)
	{
		UIViewRoot viewRoot =  FacesContext.getCurrentInstance().getViewRoot();
	    UIComponent comp = viewRoot.findComponent(id);
	    
	    return comp;
	}
	public void printError(String componentId, String title, String message)
	{
		FacesContext.getCurrentInstance().addMessage(componentId, new FacesMessage(FacesMessage.SEVERITY_ERROR,title, message ));
	}
	public void printError(String title, String message)
	{
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,title, message ));
	}
	
	public void printWarning(String title, String message)
	{
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,title, message ));
	}
	
	public void printSuccess(String title, String message)
	{
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,title, message ));
	}

	public ValueExpression createValueExpression(String expressionString)
	{
		ELContext elContext = FacesContext.getCurrentInstance().getELContext();
		ValueExpression val1 = FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createValueExpression( elContext, expressionString, Object.class);
		return val1;
	}
}
